// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PROD_SAVED_PRODUCT_TECHNIC
// * CreateDate    : 2011/8/16 10:16:41
// * Creater       : CodeSmith
// * LastChangeDate: 2011/8/16 10:16:41
// * LastChanger   : 2011/8/16 10:16:41
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.Business.Prod
{
    /// <summary>
    /// ProdSavedProductTechnic
    /// </summary>
    public partial class ProdSavedProductTechnicBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objProdSavedProductTechnicEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ProdSavedProductTechnicEntity GetProdSavedProductTechnic(ProdSavedProductTechnicEntity objProdSavedProductTechnicEntity)
        {
            return DataAccess.SelectSingle<ProdSavedProductTechnicEntity>(objProdSavedProductTechnicEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objProdSavedProductTechnicEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertProdSavedProductTechnic(ProdSavedProductTechnicEntity objProdSavedProductTechnicEntity)
        {
            return DataAccess.Insert(objProdSavedProductTechnicEntity);
        }

        public static bool InsertProdSavedProductTechnic(List<ProdSavedProductTechnicEntity> objProdSavedProductTechnicEntityList)
        {
            return DataAccess.Insert(objProdSavedProductTechnicEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objProdSavedProductTechnicEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateProdSavedProductTechnic(ProdSavedProductTechnicEntity objProdSavedProductTechnicEntity)
        {
            return DataAccess.Update(objProdSavedProductTechnicEntity);
        }

        public static bool UpdateProdSavedProductTechnic(List<ProdSavedProductTechnicEntity> objProdSavedProductTechnicEntityList)
        {
            return DataAccess.Update<ProdSavedProductTechnicEntity>(objProdSavedProductTechnicEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objProdSavedProductTechnicEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteProdSavedProductTechnic(ProdSavedProductTechnicEntity objProdSavedProductTechnicEntity)
        {
            return DataAccess.Delete(objProdSavedProductTechnicEntity);
        }

        public static bool DeleteProdSavedProductTechnic(List<ProdSavedProductTechnicEntity> objProdSavedProductTechnicEntityList)
        {
            return DataAccess.Delete<ProdSavedProductTechnicEntity>(objProdSavedProductTechnicEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetProdSavedProductTechnicAll()
        {
            return DataAccess.SelectAllDataSet<ProdSavedProductTechnicEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ProdSavedProductTechnicEntity> GetProdSavedProductTechnicAllList()
        {
            return DataAccess.SelectAll<ProdSavedProductTechnicEntity>();
        }
        #endregion

        #region Functions
        public static DataTable GetProdSavedProductTechnicByCondition(string rowCount, string condition, string orderBy)
        {
            string sql = CommonFunctions.GetSelect(rowCount) + @"A.* FROM PROD_SAVED_PRODUCT_TECHNIC A WHERE 1=1 ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            if (string.IsNullOrEmpty(orderBy) == false)
            {
                sql += " ORDER BY " + orderBy;
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }

        public static bool SaveFlow(bool isNew, ProdSavedProductTechnicEntity main, List<ProdSavedProductTechnicItemEntity> detail, List<ProdSavedProductTechnicItemEntity> detailHistory)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (isNew)
                    {
                        DataAccess.Insert(main, broker);
                    }
                    else
                    {
                        DataAccess.Update(main, broker);
                    }
                    //Save items
                    if (isNew)
                    {
                        DataAccess.Insert<ProdSavedProductTechnicItemEntity>(detail, broker);
                    }
                    else
                    {
                        DataAccess.Delete<ProdSavedProductTechnicItemEntity>(detailHistory, broker);
                        DataAccess.Insert<ProdSavedProductTechnicItemEntity>(detail, broker);
                    }
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static bool DeleteFlow(ProdSavedProductTechnicEntity mProdSavedProductTechnic)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    List<ProdSavedProductTechnicItemEntity> listProdSavedProductTechnicItem = ProdSavedProductTechnicItemBs.GetFlowBySavedProductTechnic(mProdSavedProductTechnic);
                    DataAccess.Delete<ProdSavedProductTechnicItemEntity>(listProdSavedProductTechnicItem, broker);
                    DataAccess.Delete(mProdSavedProductTechnic, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static bool SaveTemplate(List<ProdProductTechnicEntity> ListFlowDetail, ProdSavedProductTechnicEntity FlowTemplate)
        {
            bool bFlag = false;
            if (ListFlowDetail != null && ListFlowDetail.Count > 0 && FlowTemplate != null)
            {
                using (DataAccessBroker broker = DataAccessFactory.Instance())
                {
                    try
                    {                        
                        ProdSavedProductTechnicEntity Template = FlowTemplate;
                        Template.ProductTechnicId = Guid.NewGuid().ToString();
                        List<ProdSavedProductTechnicItemEntity> list = new List<ProdSavedProductTechnicItemEntity>();
                        for (int i = 0; i < ListFlowDetail.Count; i++)
                        {
                            ProdSavedProductTechnicItemEntity mm = new ProdSavedProductTechnicItemEntity();
                            mm.ProductTechnicItemId = Guid.NewGuid().ToString();
                            mm.ProductTechnicId = Template.ProductTechnicId;
                            mm.TechnicId = ListFlowDetail[i].TechnicId;
                            mm.Sequence = i;
                            mm.GroupId = ListFlowDetail[i].GroupId;
                            mm.Remark = ListFlowDetail[i].Remark;
                            list.Add(mm);
                        }
                        broker.BeginTransaction();
                        if (ProdSavedProductTechnicBs.InsertProdSavedProductTechnic(Template))
                        {
                            if (ProdSavedProductTechnicItemBs.InsertProdSavedProductTechnicItem(list))
                                bFlag = true;
                        }
                        broker.Commit();
                    }
                    catch (Exception ex)
                    {
                        broker.RollBack();
                        throw ex;
                    }
                }
            }
            return bFlag;
        }
        #endregion
    }


}

